package repo

import (
	"easy-note/models"
	"gorm.io/gorm"
)

var DB *gorm.DB

type UserRepo struct{}

func (userRepo *UserRepo) InsertUser(user *models.User) (bool, error) {
	db := DB.Create(user)
	err := db.Error
	if err != nil {
		return false, err
	}
	return db.RowsAffected > 0, nil
}

func (userRepo *UserRepo) GetUserByUsername(username string) (*models.User, error) {
	var user *models.User
	err := DB.First(&user, &models.User{Username: username}).Error
	if err != nil {
		return nil, err
	}
	return user, nil
}

func (userRepo *UserRepo) CheckUser(username, password string) (bool, error) {
	var count int64 = 0
	err := DB.Where(&models.User{Username: username, Password: password}).First(&models.User{}).Count(&count).Error
	if err != nil {
		return false, err
	}
	return count > 0, nil
}
